package com.hy.frame.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

/**
 * title QRoundImage
 * author HeYan
 * time 21-3-29 下午2:57
 * desc 无
 */
public class QRoundImage extends androidx.appcompat.widget.AppCompatImageView implements IAutoDesign {

    protected DesignImageHelper designHelper;

    public QRoundImage(Context context) {
        this(context, null);
    }

    public QRoundImage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QRoundImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs, defStyleAttr);
    }

    @SuppressLint("CustomViewStyleable")
    public void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        if (designHelper == null) {
            designHelper = new DesignImageHelper(this);
        }
        designHelper.loadAttributes(attrs, defStyleAttr);
        designHelper.loadBackgroundAttributes(attrs, defStyleAttr);
        designHelper.loadImageAttributes(attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        designHelper.onLayout(getWidth(), getHeight());
    }

    @Override
    public void draw(Canvas canvas) {
        //图片剪切层
        designHelper.draw(canvas);
        super.draw(canvas);
        //画边框
        designHelper.drawBorder(canvas);
    }

    /**
     * 设置圆角大小
     *
     * @param radius
     */
    public void setRadius(float radius) {
        designHelper.setRadius(radius);
        invalidate();
    }

    @Override
    public boolean isScaleEnabled() {
        return designHelper.scaleEnabled;
    }

    @Override
    public DesignHelper getDesignHelper() {
        return designHelper;
    }
}
